package com.xiumu.springbootlearn.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xiumu.springbootlearn.exception.BaseException;
import com.xiumu.springbootlearn.exception.CommonException;
import com.xiumu.springbootlearn.model.entity.User;
import com.xiumu.springbootlearn.model.vo.ResultJSON;
import org.springframework.web.bind.annotation.*;

/**
 * @author xiumu
 * @date 2021年07月04日 23:52
 */
@RestController
@RequestMapping("user")
public class UserController {

    @GetMapping("login")
    public ResultJSON login(@RequestBody User user) {
        System.out.println(user.getUsername() + "::" + user.getPassword());
        return new ResultJSON(200, "success");
    }

    @PostMapping("loginFail")
    public ResultJSON loginFail(@RequestBody User user) {

        //判断用户是否为空
        if (user == null) {
            throw new BaseException(CommonException.LOGIN_FAIL);
        }else if (!"xiumu".equals(user.getUsername())) {
            throw new BaseException(CommonException.LOGIN_FAIL);
        }

        return new ResultJSON(200, "success");
    }

    @PostMapping("noAuth")
    public ResultJSON noAuth(@RequestBody User user) {

        //判断用户是否为空
        if (user == null) {
            throw new BaseException(CommonException.LOGIN_FAIL);
        } else if (!"xiumu".equals(user.getUsername())) {
            throw new BaseException(CommonException.AUTHORITY_Fail);
        }

        return new ResultJSON(200, "success");
    }

    @GetMapping("getOneUser")
    public ResultJSON getOneUser(){
        User user = new User().setId(IdWorker.getId())
                .setUsername("朽木")
                .setPassword("123456")
                .setBankCard(IdWorker.getId());
        return ResultJSON.success(user);
    }

}
